<template>
  <div id="show-blog" v-theme:columm="'narrow'">
    <h1>博客总览</h1>
    <input type="text" v-model="search" placeholder="搜索">
    <div class="single-blog" v-for="blog in filteredBlogs">
      <router-link :to="'/blog/' + blog.id">
        <h2 v-rainbow>
          {{blog.title | to-uppercase}}
        </h2>
        <article> {{blog.body | snippet}}</article>
      </router-link>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'ShowBlog',
    data() {
      return {
        blogs: [],
        search: ''
      }
    },
    created() {
      // this.$http.get("./../static/blog.json")
      this.$http.get("https://jsonplaceholder.typicode.com/posts")
        .then(function (data) {
          this.blogs = data.body.slice(0, 10);
        })
    },
    computed: {
      filteredBlogs() {
        return this.blogs.filter((blog) => {
          return blog.title.match(this.search)
        })
      }
    },
    filters: {
      toUppercase(value) {
        return value.toUpperCase()
      }
    },
    directives: {
      'rainbow': {
        bind(el, binding, vnode) {
          el.style.color = "#" + Math.random().toString(16).slice(2, 8)
        }
      }
    }
  }
</script>

<style scoped>
  #show-blog {
    max-width: 800px;
    margin: 0 auto;
  }

  .single-blog {
    padding: 20px;
    margin: 20px 0;
    box-sizing: border-box;
    background: #eee;
    border: 1px dotted #aaa;
  }

  #show-blog a{
    color: #444;
    text-decoration: none;
  }

  input[type="text"]{
    padding: 8px;
    width: 100%;
    box-sizing: border-box;
  }

</style>
